﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;


namespace StarTrekFileBrowser
{
    public class FileDataTreeNode
    {
        private FileDataTreeNode _parent;
        public FileDataTreeNode Parent { get { return _parent; } }

        private FileDataTree _tree;
        public FileDataTree Tree { get { return _tree; } }

        public System.Drawing.Bitmap Image { get; set; }

        public List<FileDataTreeNode> Children { get; set; }
        public int ID { get; set; }
        public string Path { get { return FullFilename.Substring(0, FullFilename.LastIndexOf("\\")+1); } }

        public string Filename { 
            get {
                if (NodeType == NodeType.FOLDER) return FullFilename.Substring(Path.Length);
                var t = FullFilename.Substring(FullFilename.LastIndexOf("\\")+1);
                return t.Substring(0, t.LastIndexOf("."));
                // FullFilename.Substring(Path.Length, FullFilename.LastIndexOf(".")); 
            } 
        
        }
        public string FileEnding { 
            get {
                if (NodeType == NodeType.FOLDER) return "";
                return FullFilename.Substring(FullFilename.LastIndexOf(".")+1);  
            } 
        }
        public string FullFilename { get; set; }
        
        private int _layer;
        public int Layer { get { return _layer; } }

        public bool IsRoot { get { return _parent == null; } }
        public bool HasChilds { get { return Children.Count > 0; } }
        public int ChildCount { get { return Children.Count; } }

        public bool ChildrenChecked { get; set; }
        public bool Aktive { get; set; }

        public NodeType NodeType { get; set; }

        public Brush FillColor { get { return (Aktive ? Brushes.WhiteSmoke : (NodeType == NodeType.FILE ? Brushes.LightSteelBlue : Brushes.Violet)); } }
        public Brush StrokeColor { get { return (Aktive ? Brushes.Yellow : Brushes.Black); } }
        public Brush LineColor { get { return (NodeType == NodeType.FOLDER ? (Aktive ? Brushes.Yellow : Brushes.Black) : Brushes.Black); } }
        public Brush FrontLineColor { get { return (Layer > 1 ? Brushes.Yellow : Brushes.Black); } }

        public bool IsQueried { get; set; }

        /// <summary>
        /// building a root node for a given tree
        /// </summary>
        /// <param name="tree"></param>
        public FileDataTreeNode(FileDataTree tree)
        {
            FullFilename = "";
            

            _parent = null;
            _tree = tree;
            _parent = null;
            _layer = Tree.LayerCount;

            Tree.Root = this;
            Children = new List<FileDataTreeNode>();
            ID = Tree.NodeCount;
            Tree.NodeCount++;

            NodeType = NodeType.FOLDER;

            ChildrenChecked = false;
            Aktive = false;

            Image = null;
            IsQueried = false;
        }

        /// <summary>
        /// building a child node for a given parent node
        /// </summary>
        /// <param name="parent"></param>
        public FileDataTreeNode(FileDataTreeNode parent)
        {
            FullFilename = "";

            _parent = parent;
            _tree = parent.Tree;
            _layer = Parent.Layer + 1;

            Tree.NodeCount++;
            if (Tree.LayerCount < Layer) Tree.LayerCount++;

            Children = new List<FileDataTreeNode>();

            ID = Tree.NodeCount;
            Tree.NodeCount++;

            //Path = Parent.Path + "\\" + Parent.FullFilename;

            Parent.Children.Add(this);

            ChildrenChecked = false;
            Aktive = false;

            Image = null;
            IsQueried = false;
        }


    }

    /// <summary>
    /// simple enum for two node types: either it represents a folder or it represents a filename
    /// </summary>
    public enum NodeType
	{
	    FOLDER,
        FILE,
        ROOT
	}

}
